package cn.tx.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import cn.tx.model.Emp;
import cn.tx.model.OrderDetail;
import cn.tx.model.OrderModel;
import cn.tx.model.Product;
import cn.tx.model.Supplier;
import cn.tx.query.OrderModelQuery;
import cn.tx.service.OrderModelService;
import cn.tx.service.SupplierService;
import cn.tx.util.ERPConstants;
import cn.tx.util.JSONWriter;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class OrderModelAction extends BaseAction implements ModelDriven<OrderModel>{
	
	private OrderModel orderModel = new OrderModel();
	
	private OrderModelService orderModelService;
	
	private SupplierService supplierService;
	


	/**
	 * 定义订单明细的接收数组
	 */
	private Integer[] productTypeId;
	private Integer[] productId;
	private Integer[] nums;
	private Double[] prices;
	
	private String note;
	
	
	
	
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public Integer[] getProductTypeId() {
		return productTypeId;
	}

	public void setProductTypeId(Integer[] productTypeId) {
		this.productTypeId = productTypeId;
	}

	public Integer[] getProductId() {
		return productId;
	}

	public void setProductId(Integer[] productId) {
		this.productId = productId;
	}

	public Integer[] getNums() {
		return nums;
	}

	public void setNums(Integer[] nums) {
		this.nums = nums;
	}



	public Double[] getPrices() {
		return prices;
	}

	public void setPrices(Double[] prices) {
		this.prices = prices;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public OrderModel getOrderModel() {
		return orderModel;
	}

	public void setOrderModel(OrderModel orderModel) {
		this.orderModel = orderModel;
	}

	/**
	 * 查询对象
	 */
	private OrderModelQuery erpQuery = new OrderModelQuery();
	
	
	public OrderModelService getOrderModelService() {
		return orderModelService;
	}

	public void setOrderModelService(OrderModelService orderModelService) {
		this.orderModelService = orderModelService;
	}
	

	public OrderModelQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(OrderModelQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	/**
	 * 查询员工的列表
	 * @return
	 */
	public String orderModel_buy_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		String orderTypeStr = erpQuery.getOrderTypeStr();
		//把逗号分隔的字符串拆分出来
		if(StringUtils.isNotBlank(orderTypeStr)){
			String[] split = orderTypeStr.split(",");
			List<Integer> ots = new ArrayList<Integer>();
			for(String str : split){
				ots.add(new Integer(str));
			}
			//把多个订单类型的数据设置上
			erpQuery.setOrderTypes(ots);
			
		}
		exclude.add("orderTypeStr");
		Page page = orderModelService.selectObjectByConditionPage(erpQuery, exclude);
		context.put("page", page);
		
		return SUCCESS;
	}
	public String orderModel_buy_checkList(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = orderModelService.selectObjectByConditionPage(erpQuery, exclude);
		context.put("page", page);
		
		return SUCCESS;
	}
	public String orderModel_buy_audit(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		return SUCCESS;
	}
	public String orderModel_buy_detailupdate(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		return SUCCESS;
	}
	public String orderModel_buy_note(){
		return SUCCESS;
	}
	
	
	public void orderModel_buy_auditOrder(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session2 = context.getSession();
		//订单的编号
		
		//从session中获得emp对象
		Emp emp = (Emp) session2.get("user");
		orderModelService.auditOrder(emp, orderModel.getOrderId(), orderModel.getOrderState(), note);
		JSONWriter.writeString("yes", response);
	}
	
	
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String orderModel_buy_input(){
		ActionContext context = ActionContext.getContext();
		List<Supplier> suppliers = supplierService.listAll();
		context.put("suppliers", suppliers);
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String orderModel_buy_preupdate(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 */
	public void orderModel_buy_updateSubmit(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session2 = context.getSession();
		//订单的编号
		String orderNum = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		orderModel.setOrderNum(orderNum);
		//从session中获得emp对象
		Emp emp = (Emp) session2.get("user");
		
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		orderModel.setOrderState(ERPConstants.AUDIT_ORDER);
		orderModel.setOrderCreater(emp);
		orderModel.setCreateTime(new Date());
		//获得之前的订单明细
		Set<OrderDetail> oldOds = orderModel.getOds();
		Set<OrderDetail> ods = new HashSet<OrderDetail>();
		//定义商品总数
		int pNum = 0;
		//总价钱
		double totalPrice = 0;
		
		for(int i = 0; i < productId.length; i++){
			OrderDetail od = new OrderDetail();
			od.setDetailNum(nums[i]);
			od.setDetailPrice(prices[i]);
			Product p = new Product();
			p.setProductId(productId[i]);
			od.setProduct(p);
			od.setSurplus(nums[i]);
			ods.add(od);
			pNum = pNum + nums[i];
			totalPrice =  totalPrice + prices[i]*nums[i];
		}
		orderModel.setOds(ods);
		orderModel.setTotalNum(pNum);
		orderModel.setTotalPrice(totalPrice);
		orderModelService.updateSubmitAuidOrder(orderModel, oldOds);
		JSONWriter.writeString("yes", response);
	}
	
	/**
	 * 订单的修改
	 */
	public void orderModel_buy_save(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session2 = context.getSession();
		//订单的编号
		String orderNum = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		orderModel.setOrderNum(orderNum);
		//从session中获得emp对象
		Emp emp = (Emp) session2.get("user");
		orderModel.setOrderCreater(emp);
		orderModel.setCreateTime(new Date());
		orderModel.setOrderType(ERPConstants.ORDER_TYPE_BUY);
		orderModel.setOrderState(ERPConstants.AUDIT_ORDER);
		
		Set<OrderDetail> ods = new HashSet<OrderDetail>();
		//定义商品总数
		int pNum = 0;
		//总价钱
		double totalPrice = 0;
		
		for(int i = 0; i < productId.length; i++){
			OrderDetail od = new OrderDetail();
			od.setDetailNum(nums[i]);
			od.setDetailPrice(prices[i]);
			Product p = new Product();
			p.setProductId(productId[i]);
			od.setProduct(p);
			od.setSurplus(nums[i]);
			ods.add(od);
			pNum = pNum + nums[i];
			totalPrice =  totalPrice + prices[i]*nums[i];
		}
		orderModel.setOds(ods);
		orderModel.setTotalNum(pNum);
		orderModel.setTotalPrice(totalPrice);
		orderModelService.save(orderModel);
		JSONWriter.writeString("yes", response);
	}
	/**
	 * 修改
	 * @return
	 */
	public String orderModel_buy_update(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		
		
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String orderModel_buy_delete(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		orderModelService.delete(orderModel);
		return TOLIST;
	}

	@Override
	public OrderModel getModel() {
		return orderModel;
	}
	
	
	
	

}
